package com.yxinsur.imc.business.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcBroker;
import com.yxinsur.imc.business.domain.ImcBrokerChangeTeam;
import com.yxinsur.imc.business.form.ImcBrokerChangeTeamForm;
import com.yxinsur.imc.business.mapper.ImcBrokerChangeTeamMapper;
import com.yxinsur.imc.business.mapper.ImcBrokerMapper;
import com.yxinsur.imc.business.service.IImcBrokerChangeTeamService;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 代理人团队异动记录Service业务层处理
 *
 * @author imc
 * @date 2021-03-23
 */
@Service
public class ImcBrokerChangeTeamServiceImpl extends ServiceImpl<ImcBrokerChangeTeamMapper, ImcBrokerChangeTeam> implements IImcBrokerChangeTeamService
{
    @Autowired
    private ImcBrokerChangeTeamMapper imcBrokerChangeTeamMapper;
    @Autowired
    private ImcBrokerMapper imcBrokerMapper;

    /**
     * 查询代理人团队异动记录
     *
     * @param id 代理人团队异动记录ID
     * @return 代理人团队异动记录
     */
    @Override
    public ImcBrokerChangeTeam selectImcBrokerChangeTeamById(String id)
    {
        return imcBrokerChangeTeamMapper.selectImcBrokerChangeTeamById(id);
    }

    /**
     * 查询代理人团队异动记录列表
     *
     * @param imcBrokerChangeTeam 代理人团队异动记录
     * @return 代理人团队异动记录
     */
    @Override
    public List<ImcBrokerChangeTeam> selectImcBrokerChangeTeamList(ImcBrokerChangeTeam imcBrokerChangeTeam)
    {
        return imcBrokerChangeTeamMapper.selectImcBrokerChangeTeamList(imcBrokerChangeTeam);
    }

    /**
     * 新增代理人团队异动记录
     *
     * @param imcBrokerChangeTeam 代理人团队异动记录
     * @return 结果
     */
    @Override
    public int insertImcBrokerChangeTeam(ImcBrokerChangeTeamForm imcBrokerChangeTeam)
    {
        String[] brokerIds = imcBrokerChangeTeam.getBrokerIds();
        Date date = new Date();
        for (String brokerId:brokerIds) {
            ImcBroker broker = imcBrokerMapper.selectImcBrokerById(brokerId);
            ImcBrokerChangeTeam change = new ImcBrokerChangeTeam();
            change.setChangeTime(imcBrokerChangeTeam.getChangeTime());
            change.setBrokerId(broker.getId());
            change.setSourceTeamId(broker.getTeamId());
            change.setTargetTeamId(imcBrokerChangeTeam.getMarkserviceId());
            change.setUpdateTime(date);
            change.setCreateId(SecurityUtils.getUsername());
            change.setCreateTime(date);
            change.setRemark(imcBrokerChangeTeam.getRemark());
            change.setTenantId(String.valueOf(SecurityUtils.getTenantId()));
            imcBrokerChangeTeamMapper.insertImcBrokerChangeTeam(change);
            //更改营服
            broker.setTeamId(imcBrokerChangeTeam.getMarkserviceId());
            imcBrokerMapper.updateImcBroker(broker);
        }
        return 1;
    }

    /**
     * 修改代理人团队异动记录
     *
     * @param imcBrokerChangeTeam 代理人团队异动记录
     * @return 结果
     */
    @Override
    public int updateImcBrokerChangeTeam(ImcBrokerChangeTeam imcBrokerChangeTeam)
    {
        imcBrokerChangeTeam.setUpdateTime(DateUtils.getNowDate());
        return imcBrokerChangeTeamMapper.updateImcBrokerChangeTeam(imcBrokerChangeTeam);
    }

    /**
     * 批量删除代理人团队异动记录
     *
     * @param ids 需要删除的代理人团队异动记录ID
     * @return 结果
     */
    @Override
    public int deleteImcBrokerChangeTeamByIds(String[] ids)
    {
        return imcBrokerChangeTeamMapper.deleteImcBrokerChangeTeamByIds(ids);
    }

    /**
     * 删除代理人团队异动记录信息
     *
     * @param id 代理人团队异动记录ID
     * @return 结果
     */
    @Override
    public int deleteImcBrokerChangeTeamById(String id)
    {
        return imcBrokerChangeTeamMapper.deleteImcBrokerChangeTeamById(id);
    }
}
